/*
 */
package com.ericsson.springintegrationtest.controller;

import com.ericsson.springintegrationtest.model.User;
import java.util.ArrayList;
import java.util.Date;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.apache.log4j.Logger;
import org.springframework.integration.Message;
import org.springframework.mail.MailMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;

/*
 * @author Singaram Subramanian
 *
 * MailTransformer constructs a simple mail message out of the payload (XML data in this case)
 * that the incoming Message instance carries with it.
 */
public class MailTransformer {

    private String mailTo;

    private String mailFrom;

    private String mailSubject;

    private JavaMailSenderImpl sender;

    private static Logger logger = Logger.getLogger(MailTransformer.class);

    public String getMailTo() {
        return mailTo;
    }

    public void setMailTo(String mailTo) {
        this.mailTo = mailTo;
    }

    public String getMailFrom() {
        return mailFrom;
    }

    public void setMailFrom(String mailFrom) {
        this.mailFrom = mailFrom;
    }

    public String getMailSubject() {
        return mailSubject;
    }

    public void setMailSubject(String mailSubject) {
        this.mailSubject = mailSubject;
    }

    public JavaMailSenderImpl getSender() {
        return sender;
    }

    public void setSender(JavaMailSenderImpl sender) {
        this.sender = sender;
    }

    public MailMessage sendProcessNotificationMessage(Message<?> message) {

        User user = (User) message.getPayload();

        logger.info("User : " + user.toString());

        MailMessage msg = new SimpleMailMessage();
        msg.setTo(mailTo);
        msg.setFrom(mailFrom);
        msg.setSubject(mailSubject);
        msg.setSentDate(new Date());
        msg.setText("Welcome " + user.getFirstName() + " " + user.getLastName() + ". You request is being process.");
        
        logger.info("Constructed the mail to be sent to the mailbox");

        return msg;
    }

    public MailMessage sendRecapNotificationMessage(Message<?> message) throws MessagingException {

        ArrayList list = (ArrayList) message.getPayload();

        StringBuilder b = new StringBuilder("Recap for file : " + message.getHeaders().get("file_originalFile"));
        b.append("<BR>");
        for (Object obj : list) {
            User user = (User)obj;
            if ( user.getId() != null ) {
                b.append(user.getFirstName() + " " + user.getLastName() + " correctly inserted.").append("<BR>");
            }
            else {
                b.append(user.getFirstName() + " " + user.getLastName() + " alread present.").append("<BR>");
            }            
        }

        MimeMessage mimeMessage = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(mailFrom);
        helper.setTo(mailTo);
        helper.setSubject(mailSubject);
        helper.setText(b.toString(), true);

        return new MimeMailMessage(mimeMessage);
    }
}
